package jmine.tec.database.query.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.Comment;
import jmine.tec.persist.annotation.Comments;
import jmine.tec.persist.annotation.Index;
import jmine.tec.persist.annotation.UniqueConstraint;
import bancosys.tec.component.Documentation;
import bancosys.tec.persist.bussobj.PersistableBusinessObject;

/**
 * Entidade "nó", possui associações para outras folhas e outros nós.
 * 
 * @author lundberg
 */
@Entity
@Table(name = "NODE_ENTITY")
@Alias("NODEEN")
@SequenceGenerator(name = "SEQ_NODEEN", sequenceName = "SEQ_NODEEN")
@Documentation("NODE_ENTITY")
public class NodeEntity extends PersistableBusinessObject {

    private Long id;

    private NodeEntity parent;

    private Set<LeafEntity> leafs = new HashSet<LeafEntity>();

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(generator = "SEQ_NODEEN")
    @Column(name = "COD_NODE")
    @Documentation("COD_NODE")
    public Long getId() {
        return this.id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the parent
     */
    @ManyToOne
    @Index(suffix = "JT_0")
    @JoinColumn(name = "COD_PARENT")
    @Documentation("COD_PARENT")
    public NodeEntity getParent() {
        return this.parent;
    }

    /**
     * @return the leafs
     */
    @OneToMany
    @JoinTable(name = "ASSOC_NODE_LEAF", joinColumns = @JoinColumn(name = "COD_NODE"), inverseJoinColumns = @JoinColumn(name = "COD_LEAF"))
    @Alias("ASNDLE")
    @UniqueConstraint(name = "AK_ASNDLE_0", columns = "COD_LEAF")
    @Comments({ @Comment(table = "ASSOC_NODE_LEAF", value = "ASSOC_NODE_LEAF"),
            @Comment(table = "ASSOC_NODE_LEAF", column = "COD_NODE", value = "COD_NODE"),
            @Comment(table = "ASSOC_NODE_LEAF", column = "COD_LEAF", value = "COD_LEAF") })
    public Set<LeafEntity> getLeafs() {
        return this.leafs;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(NodeEntity parent) {
        this.parent = parent;
    }

    /**
     * @param leafs the leafs to set
     */
    public void setLeafs(Set<LeafEntity> leafs) {
        this.leafs = leafs;
    }

    /**
     * Adiciona uma folha
     * 
     * @param leaf leaf
     */
    public void addLeaf(LeafEntity leaf) {
        this.leafs.add(leaf);
    }
}
